using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using System.ComponentModel.Design;
using Microsoft.Practices.RecipeFramework;
using Microsoft.Practices.Common;
using EnvDTE;
using EnvDTE80;
using Microsoft.Practices.ComponentModel;
using Microsoft.Practices.RepositoryFactory.CommonHelpers;

namespace Microsoft.Practices.RepositoryFactory.ValueProviders
{
    /// <summary>
    /// A value provider that gives a list of the projects in a solution
    /// that have a particular responsibility assigned. The specific
    /// responsibility that we look for is supplied in the configuration.
    /// </summary>
    [ServiceDependency(typeof(DTE))]
    public class ProjectsWithResponsibilityProvider : ValueProvider, IAttributesConfigurable
    {
        private const string ResponsibilityArgument = "ResponsibilityArgument";
        private const string CurrentProjectArgument = "CurrentProjectArgument";
        StringDictionary configuration;

        /// <summary>
        /// Called when [begin recipe].
        /// </summary>
        /// <param name="currentValue">The current value.</param>
        /// <param name="newValue">The new value.</param>
        /// <returns></returns>
        public override bool OnBeginRecipe(object currentValue, out object newValue)
        {
            return GetValue(out newValue);
        }

        private Project GetCurrentProject()
        {
            Project returnProject = null;

            DTE service = (DTE)this.GetService(typeof(DTE));
            if (service.SelectedItems.Count == 1)
            {
                SelectedItem item = service.SelectedItems.Item(1);
                if (item.Project != null)
                {
                    returnProject = item.Project;
                }
            }
            return returnProject;
        }


        private bool GetValue(out object newValue )
        {
            ResponsibleProjectFinder finder = new ResponsibleProjectFinder((DTE)(Site.GetService(typeof(DTE))));
            newValue = null;

            Project curProject = null;

            if (configuration.ContainsKey(CurrentProjectArgument))
            {
                IDictionaryService dictionary = (IDictionaryService)GetService(typeof(IDictionaryService));
                string currentProjectArgName = configuration[CurrentProjectArgument];
                curProject = (Project)dictionary.GetValue(currentProjectArgName);
            }

            if (curProject == null)
            {
                curProject = GetCurrentProject();
            }
            string resp = configuration[ResponsibilityArgument];


            List<Project> projects = finder.FindProjectsWithResponsibility(resp);

            if (projects.Count > 0)
            {
                if (curProject != null)
                {
                    foreach (Project testProj in projects)
                    {
                        if (testProj.Name == curProject.Name)
                        {
                            newValue = testProj;
                            continue;
                        }
                    }
                }

                if (newValue == null)
                {
                    newValue = projects[0];
                }

            }
            else
            {
                newValue = null;
            }
            return true;
        }

        #region IAttributesConfigurable Members

        /// <summary>
        /// Configures the specified attributes.
        /// </summary>
        /// <param name="attributes">The attributes.</param>
        public void Configure(System.Collections.Specialized.StringDictionary attributes)
        {
            if (attributes == null)
                throw new ArgumentNullException("attributes");
            
            if (!attributes.ContainsKey(ResponsibilityArgument))
            {
                throw new ArgumentException(Properties.Resources.InvalidArgument, ResponsibilityArgument);
            }
           
            configuration = attributes;
        }

        #endregion
    }
}
